#include "GraphicsManager.h"
#ifdef _WIN32
#include "DirectX9/GraphicsSetFactoryDX9.h"
#include "DirectX9/LostResource/LostResourceManager.h"
#endif

using namespace Stray;
using namespace Stray::Core;
using namespace Stray::Core::Graphics;

void GraphicsManagerImpl::initialize()
{
#ifdef _WIN32
	DirectX9::LostResourceManager::getInstance().initialize();
#endif
}

void GraphicsManagerImpl::terminate()
{
#ifdef _WIN32
	DirectX9::LostResourceManager::getInstance().terminate();
#endif
}

DisplayModeArray GraphicsManagerImpl::getEnableDisplayModes()
{
#ifdef _WIN32
	DisplayModeArray output;
	IDirect3D9 *pD3D9 = Direct3DCreate9(D3D_SDK_VERSION);
	Uint32 modeCount = pD3D9->GetAdapterModeCount(D3DADAPTER_DEFAULT, D3DFMT_X8R8G8B8);
	for(Uint32 i=0; i<modeCount; ++i)
	{
		D3DDISPLAYMODE mode;
		pD3D9->EnumAdapterModes(
			D3DADAPTER_DEFAULT,
			D3DFMT_X8R8G8B8,
			i,
			&mode);
		if(output.size() != 0 
			&& mode.Width == output.back().width_
			&& mode.Height == output.back().height_)
			continue;
		DisplayMode m;
		m.width_ = mode.Width;
		m.height_ = mode.Height;
		output.push_back(m);
	}
	return output;
#else
	return DisplayModeArray();
#endif 
}

GraphicsSet GraphicsManagerImpl::createGraphicsSet(const WindowHandle& handle, const GraphicsParameter& param)
{
	GraphicsSet output;

	switch(param.api_)
	{
#ifdef _WIN32
	case GAPI_DIRECTX9:
	{
		DirectX9::GraphicsSetFactoryDX9 factory(handle, param);
		output.spContext_ = factory.getContext();
		output.spDevice_ = factory.getDevice();
		output.spView_ = factory.getView();
	}
		break;
	case GAPI_DIRECTX11:
	{
	}
		break;
#endif
	case GAPI_OPENGL:
		break;
	default:
		assert(false);
		return GraphicsSet();
	}

	return output;
}